package Presentation;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.GregorianCalendar;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;

import sun.util.calendar.LocalGregorianCalendar.Date;

import Logic.User;

public class UserDetailsPanel extends JPanel{
	
	private int firstCol = 20;
	private int secondCol = 120;
	private int y = 75;
	private int dy = 30;
	private int height = 20;
	private int widthF = 100;
	private int widthS = 200;
	private String username,password,firstName,lastName,emailAddress,gender,birthDate;
	
	public UserDetailsPanel(final MainFrame main, User user, String header, boolean editable){
		super();		
		setLayout(null);
		setSize(350,400);
		setBorder(new LineBorder(Color.black));
		if (user != null){
			username = user.getUsername();
			password = "stam12";//user.getPassword();
			firstName = user.getFirstName();
			lastName = user.getLastName();
			emailAddress = user.getEmailAddress();
			gender = user.getGender();
			birthDate = user.getBirthDate();
		}
		
		JLabel headerL = new MyLabel(header,300,50,25,10);
		headerL.setHorizontalAlignment(SwingConstants.CENTER);
		headerL.setVerticalAlignment(SwingConstants.CENTER);
		headerL.setFont(new Font("Arial",Font.BOLD,20));		
		add(headerL);		
		
		JLabel usernameL = new MyLabel("Username",widthF,height,firstCol,y);
		add(usernameL);		
		final JTextField usernameT = new MyTextField(username,widthS,height,secondCol,y,editable);
		add(usernameT);
		y += dy;
		
		JLabel passwordL = new MyLabel("Password",widthF,height,firstCol,y);
		add(passwordL);		
		final JPasswordField passwordT = new MyPasswordField(password,widthS,height,secondCol,y,editable);		
		add(passwordT);
		y += dy;
		
		JLabel retypeL = new MyLabel("Retype Password",widthF,height,firstCol,y);
		add(retypeL);		
		final JPasswordField retypeT = new MyPasswordField(password,widthS,height,secondCol,y,editable);
		add(retypeT);
		y += dy;
		
		JLabel firstL = new MyLabel("First Name",widthF,height,firstCol,y);
		add(firstL);		
		final JTextField firstT = new MyTextField(firstName,widthS,height,secondCol,y,editable);
		add(firstT);
		y += dy;
		
		JLabel lastL = new MyLabel("Last Name",widthF,height,firstCol,y);
		add(lastL);		
		final JTextField lastT = new MyTextField(lastName,widthS,height,secondCol,y,editable);
		add(lastT);
		y += dy;
		
		JLabel emailL = new MyLabel("Email Address",widthF,height,firstCol,y);
		add(emailL);		
		final JTextField emailT = new MyTextField(emailAddress,widthS,height,secondCol,y,editable);
		add(emailT);
		y += dy;
		
		JLabel genderL = new MyLabel("Gender",widthF,height,firstCol,y);
		add(genderL);
		String[] genders = {"Male","Female"};
		final JComboBox genderC = new JComboBox(genders);
		genderC.setSize(widthF, height);
		genderC.setLocation(secondCol, y);
		genderC.setSelectedItem(gender);
		genderC.setEditable(editable);
		add(genderC);
		y += dy;
		
		JLabel birthL = new MyLabel("Birthday",widthF,height,firstCol,y);
		add(birthL);		
		final JTextField birthT = new MyTextField(birthDate,widthS,height,secondCol,y,editable);
		add(birthT);
		y += dy;
		
		if (user == null){
			JButton addB = new MyButton("Add",100,30,125,y);
			add(addB);
			addB.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					String[] vals = new String[9];					
					vals[0] = usernameT.getText(); 
					String password = "";
					for (char c : passwordT.getPassword())
						password += c;
					vals[1] = password;
					vals[2] = firstT.getText();
					vals[3] = lastT.getText();
					vals[4] = emailT.getText();
					vals[5] = (String)genderC.getSelectedItem();
					vals[6] = birthT.getText();					
					vals[7] = "";
					vals[8] = "";					
					main.addNewUser(vals);
				}
			});
		}
		else {
			JButton editB = new MyButton("Edit",100,30,125,y);
			add(editB);
			editB.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					
				}
			});
			
		}	
		
	}
}
